package tw.asap.plus.ui.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import java.util.ArrayList;

import butterknife.ButterKnife;
import butterknife.InjectView;
import tw.asap.plus.R;
import tw.asap.plus.entity.CartBean;
import tw.asap.plus.entity.SubCartBean;
import tw.asap.plus.entity.response.GoodsDetailNV;
import tw.asap.plus.presenter.CartPresenter;
import tw.asap.plus.presenter.CartPresenterImpl;
import tw.asap.plus.ui.custom.QuantityController;
import tw.asap.plus.ui.custom.Tag;
import tw.asap.plus.ui.custom.TagView;
import tw.asap.plus.utils.Utils;

/**
 * Description:
 * Created:      [15/12/2]
 * Author:       [Yang]
 **/
public class CartListAdapter extends BaseAdapter {

    private Context context;
    private ArrayList<CartBean> goodsList;
    private CartPresenter cartPresenter;

    public CartListAdapter(Context context, ArrayList<CartBean> goodsList, CartPresenter cartPresenter) {
        this.context = context;
        this.goodsList = goodsList;
        this.cartPresenter = cartPresenter;
    }

    @Override
    public int getCount() {
        return goodsList == null ? 0 : goodsList.size();
    }

    @Override
    public Object getItem(int position) {
        return goodsList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.item_cart_view, null);
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
            convertView.setLayoutParams(params);
            viewHolder = new ViewHolder(convertView);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        int num = Integer.parseInt(goodsList.get(position).item_nums);
        int price = Integer.parseInt(goodsList.get(position).goodsInfo.itemEntity.getPrice());
        int totalAmt = (num * price);
        viewHolder.goodsAmt.setText("$".concat(Utils.getFormatAmt(String.valueOf(totalAmt))));
        viewHolder.goodsName.setText(goodsList.get(position).goodsInfo.itemEntity.getTitle());
        viewHolder.subLayout.removeAllViews();
        if (!goodsList.get(position).goodsInfo.hasSub) {
            viewHolder.quantityController.setVisibility(View.VISIBLE);
            viewHolder.quantityController.setStatus(num);
        } else if (goodsList.get(position).goodsInfo.subList != null && goodsList.get(position).goodsInfo.subList.size() > 0) {
            viewHolder.quantityController.setVisibility(View.INVISIBLE);
            for (SubCartBean bean : goodsList.get(position).goodsInfo.subList) {
                RelativeLayout subItemView = (RelativeLayout) LayoutInflater.from(context).inflate(R.layout.item_subgoods, null);
                QuantityController controller = (QuantityController) subItemView.findViewById(R.id.quantity_controller);
                controller.setStatus(bean.getCount());
                controller.setOnAddListener(new QuantityController.QuantityChangeListener() {
                    @Override
                    public void onChange(int newQuantity) {
                        SubCartBean newBean = new SubCartBean();
                        newBean.setCount(1);
                        newBean.setGoodsId(bean.getGoodsId());
                        newBean.setPropertyMap(bean.getPropertyMap());
                        ((CartPresenterImpl)cartPresenter).addSubItem(newBean, goodsList.get(position).goodsInfo,true);
                        notifyDataSetChanged();
                    }
                });
                controller.setOnMinusListener(new QuantityController.QuantityChangeListener() {
                    @Override
                    public void onChange(int newQuantity) {
                        SubCartBean newBean = new SubCartBean();
                        newBean.setCount(1);
                        newBean.setGoodsId(bean.getGoodsId());
                        newBean.setPropertyMap(bean.getPropertyMap());
                        ((CartPresenterImpl)cartPresenter).minusSubItem(newBean, goodsList.get(position).goodsInfo,true);
                        notifyDataSetChanged();
                    }
                });
                TagView tagView = (TagView) subItemView.findViewById(R.id.tagView);
                for (GoodsDetailNV.PropsEntity.ValuesEntity value : bean.getPropertyMap().values()) {
                    Tag tag = new Tag(value.getValue());
                    tag.layoutColor = context.getResources().getColor(R.color.asap_green);
                    tag.layoutColorPress = context.getResources().getColor(R.color.asap_green);
                    tag.tagTextColor = context.getResources().getColor(R.color.white);
                    tag.layoutBorderColor = context.getResources().getColor(R.color.palette_gray_light);
                    tag.layoutBorderSize = 1l;
                    tag.radius = 10l;
                    tagView.addTag(tag);
                }
                LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
                viewHolder.subLayout.addView(subItemView, params);
            }
        }

        viewHolder.quantityController.setOnMinusListener(newQuantity -> {
            if (cartPresenter != null) {
                cartPresenter.minus(goodsList.get(position).goodsInfo);
            }
        });
        viewHolder.quantityController.setOnAddListener(newQuantity -> {
            if (cartPresenter != null) {
                cartPresenter.add(goodsList.get(position).goodsInfo);
            }
        });
        return convertView;
    }

    public void clearCart() {
        cartPresenter.clearCart();
    }


    static class ViewHolder {
        @InjectView(R.id.goods_name)
        TextView goodsName;
        @InjectView(R.id.goods_amt)
        TextView goodsAmt;
        @InjectView(R.id.quantity_controller)
        QuantityController quantityController;
        @InjectView(R.id.sub_layout)
        LinearLayout subLayout;

        ViewHolder(View view) {
            ButterKnife.inject(this, view);
        }
    }
}
